package task

import (
	"context"
	"engine/consts"
	emodel "engine/model"
	"fmt"
	"github.com/goccy/go-json"
	"github.com/hibiken/asynq"
	"lib/config"
	"lib/log"
)

var queue *asynq.Client

func InitQueueClient(rcf *config.Redis) {
	if queue != nil {
		return
	}
	queue = asynq.NewClient(asynq.RedisClientOpt{
		Network:  rcf.Network,
		Addr:     fmt.Sprintf("%s:%s", rcf.Address, rcf.Port),
		Password: rcf.Password,
		DB:       rcf.Database,
	})
}

func Enqueue(task *asynq.Task, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	return queue.Enqueue(task, opts...)
}

func EnqueueNew(typename string, data any, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}
	return queue.Enqueue(asynq.NewTask(typename, payload), opts...)
}

func EnqueueContext(ctx context.Context, task *asynq.Task, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	return queue.EnqueueContext(ctx, task, opts...)
}

func SendEmail(user_id int64, money float64, content string, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendEmailPayload{
		UserId:  user_id,
		Money:   money,
		Content: content,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.SendEmail), payloadJson), opts...)
}

func SendUserLevel(user_id uint64, days int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendUserLevel{
		UserId: user_id,
		Days:   days,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.SyncCoreDataJson), payloadJson), opts...)
}

func SendUserLevelNow(id int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendUserLevelNow{
		Id: id,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}
	return Enqueue(asynq.NewTask(string(consts.SyncFlightOrder), payloadJson), opts...)
}

func SendUserLevelNowOrder(id int, opts ...asynq.Option) (*asynq.TaskInfo, error) {

	payload := emodel.SendUserLevelNow{
		Id: id,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}
	return Enqueue(asynq.NewTask(string(consts.SyncFlightOrderData), payloadJson), nil)
}

func SendXuanShang(user_id int64, money float64, content string, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendEmailPayload{
		UserId:  user_id,
		Money:   money,
		Content: content,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.Xuanshang), payloadJson), opts...)
}
func SendEmsTask(oid int64, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendEmailPayload{
		UserId: oid,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.Ems), payloadJson), opts...)
}
func SendZhushouOrderDea(id, uid uint64, orderNo, skuids string, status int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendZhushouPayload{
		Id:      id,
		OrderNo: orderNo,
		Skuids:  skuids,
		Uid:     uid,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.ZhuShouDea), payloadJson), opts...)
}

func SendZhushouOrderDeaWw(id, uid uint64, orderNo, skuids string, status int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendZhushouPayload{
		Id:      id,
		OrderNo: orderNo,
		Skuids:  skuids,
		Uid:     uid,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.ZhuShouDeaWw), payloadJson), opts...)
}

func SendDingdanLevel(id, uid uint64, num int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.SendDingdanLevelload{
		Id:  id,
		Num: num,
		Uid: uid,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.DingdanLevel), payloadJson), opts...)
}
func SendDingdanLevelGamesss(payload emodel.My_game_day, opts ...asynq.Option) (*asynq.TaskInfo, error) {

	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.DingdanLevelGamesss), payloadJson), opts...)
}

func SendFlishGame(uid uint64, days int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.FlishTaskFenhong{
		Days: days,
		Uid:  uid,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.Chanchu), payloadJson), opts...)
}

func SendTzFenhong(uid uint64, days int, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.FlishTaskFenhong{
		Uid:  uid,
		Days: days,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.Fenhong), payloadJson), opts...)
}

func SendTzFenhongFlish(uid uint64, days int, num float64, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.FlishTaskFenhong{
		Uid:  uid,
		Days: days,
		Num:  num,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.FenhongFlish), payloadJson), opts...)
}

func SendUserMoneyAdd(uid uint64, num float64, flag, types int, str string, opts ...asynq.Option) (*asynq.TaskInfo, error) {
	payload := emodel.UserAddMoney{
		Uid:  uid,
		Num:  num,
		Flag: flag,
		Str:  str,
		Type: types,
	}
	payloadJson, err := json.Marshal(payload)
	if err != nil {
		log.Errorf("json编码失败：%v", err)
		return nil, err
	}

	return Enqueue(asynq.NewTask(string(consts.SyncUserAddMoney), payloadJson), opts...)
}
